package com.hcms.system.service.impl;

import com.hcms.common.utils.tree.TreeNode;
import com.hcms.common.utils.tree.TreeUtil;
import com.hcms.system.dao.MenuDao;
import com.hcms.system.dao.RoleMenuDao;
import com.hcms.system.domain.MenuDataDto;
import com.hcms.system.domain.MenuDo;
import com.hcms.system.domain.RoleMenuDo;
import com.hcms.system.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDao menuMapper;

    @Autowired
    RoleMenuDao roleMenuMapper;

    @Override
    public MenuDo get(int id) {
        MenuDo menuDo = menuMapper.get(id);
        return menuDo;
    }

    @Override
    public MenuDo getByPermsName(String permsName) {
        MenuDo menuDo = menuMapper.getByPermsName(permsName);
        return menuDo;
    }

    @Override
    public List<MenuDo> list(Map<String, Object> map) {
        List<MenuDo> menus = menuMapper.list(map);
        return menus;
    }

    @Override
    public int count(Map<String, Object> map) {
        int count = menuMapper.count(map);
        return count;
    }

    @Override
    public int save(MenuDo menu) {
        Date date = new Date();
        menu.setCreateTime(date);
        menu.setUpdateTime(date);
        int count = menuMapper.save(menu);
        return count;
    }

    @Override
    public int update(MenuDo menu) {
        menu.setUpdateTime(new Date());
        int count = menuMapper.update(menu);
        return count;
    }

    @Override
    public List<MenuDo> tree() {
        List<MenuDo> menus = menuMapper.list(new HashMap<>());
        return new TreeUtil().getTree(menus);
    }

    // 获取后台菜单
    @Override
    public List<MenuDataDto> getMenuData(int roleId) {
        // 获取角色菜单
        List<RoleMenuDo> roleMenus = roleMenuMapper.listByRoleId(roleId)
                                    .stream()
                                    .filter(t -> t.getMarkStatus() == 0)
                                    .collect(Collectors.toList());

        // 获取菜单列表
        List<MenuDo> menus = new ArrayList<>();
        for (RoleMenuDo roleMenu : roleMenus) {
            MenuDo menu = menuMapper.get(roleMenu.getMenuId());
            menus.add(menu);
        }

        // 全部菜单列表
        List<MenuDo> allMenus = menuMapper.list(new HashMap<>());

        List<MenuDo> result = allMenus.stream()
                .filter(t -> (t.getType() == 0 || t.getType() == 1) && t.getMarkStatus() == 0)
                .collect(Collectors.toList());

        // 构建返回菜单数据
        List<MenuDataDto> menuData = new ArrayList<>();
        for (MenuDo item : result) {
            // 目录
            if (item.getType() == 0 && item.getId() != 1) {
                boolean isAllow = menus.stream()
                        .filter(t -> t.getParentPath().contains("," + item.getId() + ","))
                        .findFirst()
                        .isPresent();

                if (isAllow) {
                    MenuDataDto firstMenu = new MenuDataDto();
                    firstMenu.setId(item.getId());
                    firstMenu.setTitle(item.getName());
                    firstMenu.setIcon("layui-icon " + item.getIcon());
                    firstMenu.setType(0);

                    menuData.add(firstMenu);
                }
            }
        }

        for (MenuDataDto item : menuData) {
            // 菜单
            List<MenuDo> list = result.stream().filter(t -> t.getParentId() == item.getId())
                    .collect(Collectors.toList());

            List<TreeNode> childrenList = new ArrayList<>();
            if (list != null && list.size() > 0) {
                for (MenuDo menu : list) {
                    boolean isAllow = menus.stream()
                            .filter(t -> t.getParentPath().contains("," + menu.getId() + ","))
                            .findFirst()
                            .isPresent();

                    if (isAllow) {
                        MenuDataDto secondMenu = new MenuDataDto();
                        secondMenu.setId(menu.getId());
                        secondMenu.setTitle(menu.getName());
                        secondMenu.setIcon("layui-icon " + menu.getIcon());
                        secondMenu.setType(1);
                        secondMenu.setOpenType("_iframe");
                        secondMenu.setHref(menu.getUrl());

                        childrenList.add(secondMenu);
                    }
                }
            }
            item.setChildren(childrenList);
        }

        return menuData;
    }
}
